package com.news.xinwen.conroller;

import com.news.xinwen.model.news;
import com.news.xinwen.service.userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
public class newsconroller {
    @Autowired
    private userservice userservice;

    @GetMapping("/selectid")
   public Object sel(String id){
      news news=  userservice.select(id);
        return news;
    }
   @PostMapping("/insert")
    public int insert(@RequestBody  news news){
       int i=  userservice.add(news);
        return i;
    }
    @PostMapping("/delete")
    public int delete(@RequestBody news news){
        String id=news.getNewsId();
        System.out.println(id);
        int i=  userservice.del(id);
        return i;
    }
    @PostMapping("/update")
    @Transactional
    public Object up(@RequestBody news news){
        int i=userservice.update(news);
        return  i;
    }

    @RequestMapping(value = "/bbb",method = RequestMethod.GET)//只接受get 否则405
    @Transactional
    public Object up(String id,String name){
        news news =new news();
        news.setNewsId(id);
        news.setNewsAuthor(name);
        int i=userservice.update(news);
            return  i;
    }
    @RequestMapping("/ccc/{id}/{status}") //resuful 请求方式
    public Object mapp(@PathVariable("id") Integer id,@PathVariable("status") Integer status){
        Map<String,Object>  map=new HashMap<>();
        map.put("id",id);
        map.put("sss",status);
        return  map;
    }
    @RequestMapping("/all")
    public Object all(){
        List<news> b= userservice.all();
        return  b;
    }
}
